home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / jpeglib5b / jcmaster.c < prev    next >
C/C++ Source or Header  |  1980-01-12  |  13KB  |  393 lines

  1. /*
  2.  * jcmaster.c
  3.  *
  4.  * Copyright (C) 1991-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains master control logic for the JPEG compressor.
  9.  * These routines are concerned with selecting the modules to be executed
  10.  * and with determining the number of passes and the work to be done in each
  11.  * pass.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /* Private state */
  20.  
  21. typedef struct {
  22.   struct jpeg_comp_master pub;    /* public fields */
  23.  
  24.   int pass_number;        /* eventually need more complex state... */
  25. } my_comp_master;
  26.  
  27. typedef my_comp_master * my_master_ptr;
  28.  
  29.  
  30. /*
  31.  * Support routines that do various essential calculations.
  32.  */
  33.  
  34. LOCAL void
  35. initial_setup (j_compress_ptr cinfo)
  36. /* Do computations that are needed before master selection phase */
  37. {
  38.   int ci;
  39.   jpeg_component_info *compptr;
  40.   long samplesperrow;
  41.   JDIMENSION jd_samplesperrow;
  42.  
  43.   /* Sanity check on image dimensions */
  44.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  45.       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
  46.     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  47.  
  48.   /* Make sure image isn't bigger than I can handle */
  49.   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  50.       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  51.     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  52.  
  53.   /* Width of an input scanline must be representable as JDIMENSION. */
  54.   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  55.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  56.   if ((long) jd_samplesperrow != samplesperrow)
  57.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  58.  
  59.   /* For now, precision must match compiled-in value... */
  60.   if (cinfo->data_precision != BITS_IN_JSAMPLE)
  61.     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  62.  
  63.   /* Check that number of components won't exceed internal array sizes */
  64.   if (cinfo->num_components > MAX_COMPONENTS)
  65.     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  66.          MAX_COMPONENTS);
  67.  
  68.   /* Compute maximum sampling factors; check factor validity */
  69.   cinfo->max_h_samp_factor = 1;
  70.   cinfo->max_v_samp_factor = 1;
  71.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  72.        ci++, compptr++) {
  73.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  74.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  75.       ERREXIT(cinfo, JERR_BAD_SAMPLING);
  76.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  77.                    compptr->h_samp_factor);
  78.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  79.                    compptr->v_samp_factor);
  80.   }
  81.  
  82.   /* Compute dimensions of components */
  83.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  84.        ci++, compptr++) {
  85.     /* For compression, we never do DCT scaling. */
  86.     compptr->DCT_scaled_size = DCTSIZE;
  87.     /* Size in DCT blocks */
  88.     compptr->width_in_blocks = (JDIMENSION)
  89.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  90.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  91.     compptr->height_in_blocks = (JDIMENSION)
  92.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  93.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  94.     /* Size in samples */
  95.     compptr->downsampled_width = (JDIMENSION)
  96.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  97.             (long) cinfo->max_h_samp_factor);
  98.     compptr->downsampled_height = (JDIMENSION)
  99.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  100.             (long) cinfo->max_v_samp_factor);
  101.     /* Mark component needed (this flag isn't actually used for compression) */
  102.     compptr->component_needed = TRUE;
  103.   }
  104.  
  105.   /* Compute number of fully interleaved MCU rows (number of times that
  106.    * main controller will call coefficient controller).
  107.    */
  108.   cinfo->total_iMCU_rows = (JDIMENSION)
  109.     jdiv_round_up((long) cinfo->image_height,
  110.           (long) (cinfo->max_v_samp_factor*DCTSIZE));
  111. }
  112.  
  113.  
  114. LOCAL void
  115. per_scan_setup (j_compress_ptr cinfo)
  116. /* Do computations that are needed before processing a JPEG scan */
  117. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  118. {
  119.   int ci, mcublks, tmp;
  120.   jpeg_component_info *compptr;
  121.   
  122.   if (cinfo->comps_in_scan == 1) {
  123.     
  124.     /* Noninterleaved (single-component) scan */
  125.     compptr = cinfo->cur_comp_info[0];
  126.     
  127.     /* Overall image size in MCUs */
  128.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  129.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  130.     
  131.     /* For noninterleaved scan, always one block per MCU */
  132.     compptr->MCU_width = 1;
  133.     compptr->MCU_height = 1;
  134.     compptr->MCU_blocks = 1;
  135.     compptr->MCU_sample_width = DCTSIZE;
  136.     compptr->last_col_width = 1;
  137.     /* For noninterleaved scans, it is convenient to define last_row_height
  138.      * as the number of block rows present in the last iMCU row.
  139.      */
  140.     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  141.     if (tmp == 0) tmp = compptr->v_samp_factor;
  142.     compptr->last_row_height = tmp;
  143.     
  144.     /* Prepare array describing MCU composition */
  145.     cinfo->blocks_in_MCU = 1;
  146.     cinfo->MCU_membership[0] = 0;
  147.     
  148.   } else {
  149.     
  150.     /* Interleaved (multi-component) scan */
  151.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  152.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  153.            MAX_COMPS_IN_SCAN);
  154.     
  155.     /* Overall image size in MCUs */
  156.     cinfo->MCUs_per_row = (JDIMENSION)
  157.       jdiv_round_up((long) cinfo->image_width,
  158.             (long) (cinfo->max_h_samp_factor*DCTSIZE));
  159.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  160.       jdiv_round_up((long) cinfo->image_height,
  161.             (long) (cinfo->max_v_samp_factor*DCTSIZE));
  162.     
  163.     cinfo->blocks_in_MCU = 0;
  164.     
  165.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  166.       compptr = cinfo->cur_comp_info[ci];
  167.       /* Sampling factors give # of blocks of component in each MCU */
  168.       compptr->MCU_width = compptr->h_samp_factor;
  169.       compptr->MCU_height = compptr->v_samp_factor;
  170.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  171.       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  172.       /* Figure number of non-dummy blocks in last MCU column & row */
  173.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  174.       if (tmp == 0) tmp = compptr->MCU_width;
  175.       compptr->last_col_width = tmp;
  176.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  177.       if (tmp == 0) tmp = compptr->MCU_height;
  178.       compptr->last_row_height = tmp;
  179.       /* Prepare array describing MCU composition */
  180.       mcublks = compptr->MCU_blocks;
  181.       if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  182.     ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  183.       while (mcublks-- > 0) {
  184.     cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  185.       }
  186.     }
  187.     
  188.   }
  189.  
  190.   /* Convert restart specified in rows to actual MCU count. */
  191.   /* Note that count must fit in 16 bits, so we provide limiting. */
  192.   if (cinfo->restart_in_rows > 0) {
  193.     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  194.     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  195.   }
  196. }
  197.  
  198.  
  199. /*
  200.  * Master selection of compression modules.
  201.  * This is done once at the start of processing an image.  We determine
  202.  * which modules will be used and give them appropriate initialization calls.
  203.  */
  204.  
  205. LOCAL void
  206. master_selection (j_compress_ptr cinfo)
  207. {
  208.   my_master_ptr master = (my_master_ptr) cinfo->master;
  209.  
  210.   initial_setup(cinfo);
  211.   master->pass_number = 0;
  212.  
  213.   /* There's not a lot of smarts here right now, but it'll get more
  214.    * complicated when we have multiple implementations available...
  215.    */
  216.  
  217.   /* Preprocessing */
  218.   if (! cinfo->raw_data_in) {
  219.     jinit_color_converter(cinfo);
  220.     jinit_downsampler(cinfo);
  221.     jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
  222.   }
  223.   /* Forward DCT */
  224.   jinit_forward_dct(cinfo);
  225.   /* Entropy encoding: either Huffman or arithmetic coding. */
  226.   if (cinfo->arith_code) {
  227. #ifdef C_ARITH_CODING_SUPPORTED
  228.     jinit_arith_encoder(cinfo);
  229. #else
  230.     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  231. #endif
  232.   } else
  233.     jinit_huff_encoder(cinfo);
  234.  
  235.   /* For now, a full buffer is needed only for Huffman optimization. */
  236.   jinit_c_coef_controller(cinfo, cinfo->optimize_coding);
  237.   jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
  238.  
  239.   jinit_marker_writer(cinfo);
  240.  
  241.   /* We can now tell the memory manager to allocate virtual arrays. */
  242.   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  243.  
  244.   /* Write the datastream header (SOI) immediately.
  245.    * Frame and scan headers are postponed till later.
  246.    * This lets application insert special markers after the SOI.
  247.    */
  248.   (*cinfo->marker->write_file_header) (cinfo);
  249. }
  250.  
  251.  
  252. /*
  253.  * Per-pass setup.
  254.  * This is called at the beginning of each pass.  We determine which modules
  255.  * will be active during this pass and give them appropriate start_pass calls.
  256.  * We also set is_last_pass to indicate whether any more passes will be
  257.  * required.
  258.  */
  259.  
  260. METHODDEF void
  261. prepare_for_pass (j_compress_ptr cinfo)
  262. {
  263.   my_master_ptr master = (my_master_ptr) cinfo->master;
  264.   int ci;
  265.   int npasses;
  266.  
  267.   /* ???? JUST A QUICK CROCK FOR NOW ??? */
  268.  
  269.   /* For now, handle only single interleaved output scan; */
  270.   /* we support two passes for Huffman optimization. */
  271.  
  272.   /* Prepare for single scan containing all components */
  273.   if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  274.     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  275.          MAX_COMPS_IN_SCAN);
  276.   cinfo->comps_in_scan = cinfo->num_components;
  277.   for (ci = 0; ci < cinfo->num_components; ci++) {
  278.     cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  279.   }
  280.  
  281.   per_scan_setup(cinfo);
  282.  
  283.   if (! cinfo->optimize_coding) {
  284.     /* Standard single-pass case */
  285.     npasses = 1;
  286.     master->pub.call_pass_startup = TRUE;
  287.     master->pub.is_last_pass = TRUE;
  288.     if (! cinfo->raw_data_in) {
  289.       (*cinfo->cconvert->start_pass) (cinfo);
  290.       (*cinfo->downsample->start_pass) (cinfo);
  291.       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  292.     }
  293.     (*cinfo->fdct->start_pass) (cinfo);
  294.     (*cinfo->entropy->start_pass) (cinfo, FALSE);
  295.     (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
  296.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  297.   } else {
  298.     npasses = 2;
  299.     switch (master->pass_number) {
  300.     case 0:
  301.       /* Huffman optimization: run all modules, gather statistics */
  302.       master->pub.call_pass_startup = FALSE;
  303.       master->pub.is_last_pass = FALSE;
  304.       if (! cinfo->raw_data_in) {
  305.     (*cinfo->cconvert->start_pass) (cinfo);
  306.     (*cinfo->downsample->start_pass) (cinfo);
  307.     (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  308.       }
  309.       (*cinfo->fdct->start_pass) (cinfo);
  310.       (*cinfo->entropy->start_pass) (cinfo, TRUE);
  311.       (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_AND_PASS);
  312.       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  313.       break;
  314.     case 1:
  315.       /* Second pass: reread data from coefficient buffer */
  316.       master->pub.is_last_pass = TRUE;
  317.       (*cinfo->entropy->start_pass) (cinfo, FALSE);
  318.       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  319.       /* We emit frame/scan headers now */
  320.       (*cinfo->marker->write_frame_header) (cinfo);
  321.       (*cinfo->marker->write_scan_header) (cinfo);
  322.       break;
  323.     }
  324.   }
  325.  
  326.   /* Set up progress monitor's pass info if present */
  327.   if (cinfo->progress != NULL) {
  328.     cinfo->progress->completed_passes = master->pass_number;
  329.     cinfo->progress->total_passes = npasses;
  330.   }
  331.  
  332.   master->pass_number++;
  333. }
  334.  
  335.  
  336. /*
  337.  * Special start-of-pass hook.
  338.  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  339.  * In single-pass processing, we need this hook because we don't want to
  340.  * write frame/scan headers during jpeg_start_compress; we want to let the
  341.  * application write COM markers etc. between jpeg_start_compress and the
  342.  * jpeg_write_scanlines loop.
  343.  * In multi-pass processing, this routine is not used.
  344.  */
  345.  
  346. METHODDEF void
  347. pass_startup (j_compress_ptr cinfo)
  348. {
  349.   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  350.  
  351.   (*cinfo->marker->write_frame_header) (cinfo);
  352.   (*cinfo->marker->write_scan_header) (cinfo);
  353. }
  354.  
  355.  
  356. /*
  357.  * Finish up at end of pass.
  358.  */
  359.  
  360. METHODDEF void
  361. finish_pass_master (j_compress_ptr cinfo)
  362. {
  363.   /* More complex logic later ??? */
  364.  
  365.   /* The entropy coder needs an end-of-pass call, either to analyze
  366.    * statistics or to flush its output buffer.
  367.    */
  368.   (*cinfo->entropy->finish_pass) (cinfo);
  369. }
  370.  
  371.  
  372. /*
  373.  * Initialize master compression control.
  374.  * This creates my own subrecord and also performs the master selection phase,
  375.  * which causes other modules to create their subrecords.
  376.  */
  377.  
  378. GLOBAL void
  379. jinit_master_compress (j_compress_ptr cinfo)
  380. {
  381.   my_master_ptr master;
  382.  
  383.   master = (my_master_ptr)
  384.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  385.                   SIZEOF(my_comp_master));
  386.   cinfo->master = (struct jpeg_comp_master *) master;
  387.   master->pub.prepare_for_pass = prepare_for_pass;
  388.   master->pub.pass_startup = pass_startup;
  389.   master->pub.finish_pass = finish_pass_master;
  390.  
  391.   master_selection(cinfo);
  392. }
  393.